<?php
class PlaceController extends Zgm_Controller_Base {

    public function init()
    {
        /* Initialize action controller here */
    }
    
    public function indexAction(){
       $pages = new Model_Places();
       // @todo note there's no check if user logged
       $identity = Zend_Auth::getInstance()->getIdentity();
       $this->view->pages = $pages->getPaginatorRows($identity['id'],
               (int) $this->getRequest()->getParam('page', 1));

       
    }

    public function addAction(){
       $form = new Form_Place();
       $this->view->form = $form;
       if ($this->_request->isPost()) {
          if ($form->isValid($this->_request->getPost())){
             $requestedAddress = $form->getValue('location');
             $geocoder = new Zgm_Geo_Geocoder();
             $responceAddress = $geocoder->lookup($requestedAddress);
             foreach ($responceAddress as $addressItem) {
                $coordinates = $addressItem->getPoint();
                // todo how these coordinates will be saved? make up an array?
                $lat = $coordinates->getLatitude();
                $lng = $coordinates->getLongitude();
                $address = $addressItem->getAddress();
             }
             // @todo what if no address found? throw an error
             $identity = Zend_Auth::getInstance()->getIdentity();
             $placesModel = new Model_Places();
             $formData = $this->getRequest()->getParams();
             $data = array('user_id' => $identity['id'],
                           'title' => $formData['title'],
                           'location' => $address,
                           'lat' => $lat,
                           'lng' =>$lng);
             $placesModel->add($data);
             // @todo show message that everything is good
          }
       }
    }
    
    public function editAction(){
      if (Model_Users::isLogged()) {
      $identity = Zend_Auth::getInstance()->getIdentity();
         if ((isset($identity))&&(!empty($identity))) {
            $userId = $identity['id'];
            $placeId = $this->getRequest()->getParam('id');
            $placesModel = new Model_Places();
            // Check user requests his/her place, not the other person's.
            $placesModel = new Model_Places();
            $placeData = $placesModel->getByPlaceAndUserId($placeId, $userId);
            $form = new Form_Place();
            $this->view->form = $form;
            $form->populate($placeData);
            // $this->_helper->FlashMessenger->setNamespace('messages')->addMessage('Saved!');
         }
      } else { echo 'you are not logged!'; }
    }
    public function deleteAction(){
      if (Model_Users::isLogged()) {
      $identity = Zend_Auth::getInstance()->getIdentity();
         if ((isset($identity))&&(!empty($identity))) {
            $userId = $identity['id'];
            $placeId = $this->getRequest()->getParam('id');
            $placesModel = new Model_Places();
            // Check user requests his/her place, not the other person's.
            $placeData = $placesModel->getByPlaceAndUserId($placeId, $userId);
            if (count($placeData) > 0) {
               //@todo maybe check everythingis really deleted?
               // Feel free to delete place location.
               $placesModel = new Model_Places();
               $placesModel->deleteByPlaceId($placeId);
            }
            // $this->_helper->FlashMessenger->setNamespace('messages')->addMessage('Deleted!');
         }
      } else { echo 'you are not logged!'; }
      $this->_redirect('/place/index');
    }

   public function getPointsAction() {
      $this->_helper->layout()->disableLayout();
      $identity = Zend_Auth::getInstance()->getIdentity();
      $placeModel =  new Model_Places();
      // Get location user pointed while registration.
      $latLngA = $placeModel->getLocationByUserId($identity['id']);
      // Get other places we added.
      // send object in correct format
      echo Zend_Json::encode(array('origin' => $latLngA, 'distanations' => $latLngA));
   }
}